//
//  LLVisitorView.swift
//  xinlangweibo
//
//  Created by 任玉林 on 16/6/25.
//  Copyright © 2016年 任玉林. All rights reserved.
//

import UIKit

import SnapKit

class LLVisitorView: UIView{
    
    //定义闭包 登录和注册按钮用
    
    var closue: (() ->())?
    
    override init(frame: CGRect) {
        
        super.init(frame: frame)
        
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    //设置子视图
    private func setupUI(){
        
        //设置背景颜色
        backgroundColor = RGB(237, green: 237, blue: 237)
        
        addSubview(feedImageView)
        addSubview(maskImageView)
        addSubview(iconImageView)
        addSubview(messageLabel)
        addSubview(loginButton)
        addSubview(registerButton)
        
        
        //设置约束
        feedImageView.snp_makeConstraints { (make) -> Void in
            
            make.center.equalTo(self)
            
        }
        
        maskImageView.snp_makeConstraints { (make) -> Void in
            
            make.center.equalTo(self)
            
        }
        
        iconImageView.snp_makeConstraints { (make) -> Void in
            
            make.center.equalTo(self)
        }
        
        messageLabel.snp_makeConstraints { (make) -> Void in
            
            make.top.equalTo(maskImageView.snp_bottom).offset(6)
            
            make.width.equalTo(224)
            
            make.height.equalTo(36)
            
            make.centerX.equalTo(maskImageView)
            
        }
        
        loginButton.snp_makeConstraints { (make) -> Void in
            
            make.size.equalTo(CGSize(width: 100, height: 36))
            
            make.top.equalTo(messageLabel.snp_bottom).offset(16)
            
            //左对齐
            make.leading.equalTo(messageLabel)
        }
        
        registerButton.snp_makeConstraints { (make) -> Void in
            
            make.size.equalTo(CGSize(width: 100, height: 36))
            
            make.top.equalTo(messageLabel.snp_bottom).offset(16)
            
            //右对齐
            make.trailing.equalTo(messageLabel)
        }
        
    }
    
    //设置圆圈动画
    func setAnim(){
        
        let anim = CABasicAnimation(keyPath: "transform.rotation")
        
        anim.duration = 10
        
        anim.toValue = M_PI * 2
        
        anim.repeatCount = MAXFLOAT
        
        //当app退到后台或者跳转控制器，动画默认结束
        anim.removedOnCompletion = false
        
        feedImageView.layer.addAnimation(anim, forKey: nil)
    
    }
    
    //设置不同访客页面的内容
    func setVisitorView(imageName: String?,title: String?){
        
        if let img = imageName,let tit = title {
            
            iconImageView.image = UIImage(named: img)
            
            messageLabel.text = tit
            
            feedImageView.hidden = true
            
        }else{
            
            setAnim()
        }
    
    }
    
    
    //懒加载子控件
    //圆圈 利用便利构造函数 快速创建了UIImageView
    private lazy var feedImageView: UIImageView = UIImageView(imageName: "visitordiscover_feed_image_smallicon")
    
    //遮罩
    private lazy var maskImageView: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_mask_smallicon"))
    
    //房子
    private lazy var iconImageView: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_house"))
    
    //文字
    private lazy var messageLabel: UILabel = {
        
        let lab = UILabel()
        
        lab.textColor = ThemeColor
        
        lab.textAlignment = .Center
        
        lab.numberOfLines = 0
        
        lab.font = UIFont.systemFontOfSize(14)
        
        lab.text = "关注一些人，回这里看看有什么惊喜关注一些人，回这里看看有什么惊喜"
        
        return lab
    }()
    
    //登录按钮
    private lazy var loginButton: UIButton = {
        
        let button = UIButton()
        
        button.addTarget(self , action: "loginButtonClick", forControlEvents: UIControlEvents.TouchUpInside)
        
        button.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: UIControlState.Normal)
        
        button.setTitle("登录", forState: UIControlState.Normal)
        
        button.setTitleColor(ThemeColor, forState: UIControlState.Normal)
        
        button.titleLabel?.font = UIFont.systemFontOfSize(14)

       return button
    }()
    
    //注册按钮
    private lazy var registerButton: UIButton = {
        
        let button = UIButton()
        
        button.addTarget(self, action: "loginButtonClick", forControlEvents: UIControlEvents.TouchUpInside)
        
        button.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: UIControlState.Normal)
        
        button.setTitle("注册", forState: UIControlState.Normal)
        
        button.setTitleColor(ThemeColor, forState: UIControlState.Normal)
        
        button.titleLabel?.font = UIFont.systemFontOfSize(14)
        
        return button
    }()
    
    @objc private func loginButtonClick(){
        
        closue?()
        
    }
      
        
        
        
    
    
    

}
